<? include ("../init.php") ?>
<? include ("../includes/class.upload.php") ?>
<?
  $project_id = $_POST['project_id'];
  $catalogue = $_POST['catalogue'];
  $projectname = $_POST['name'];
  $date = $_POST['date'];
  $area = $_POST['area'];
  $owner = $_POST['owner'];
  $year = $_POST['year'];
  $value = $_POST['value'];
  
  $img_sql = '';
  
  if(isset($_FILES["project_img"]) && is_uploaded_file($_FILES["project_img"]["tmp_name"]) && $_FILES["project_img"]["tmp_name"]!=''){
    $image = new image();
  
    ini_set("html_errors", "0");
    
    // Check the upload
    if ($_FILES["project_img"]["error"] != 0) {
      echo "ERROR:invalid upload";
      exit(0);
    }
  
    $name = get_file_name($project_id, $_FILES['project_img']['tmp_name']);
    $path = "../../images/projects/".$project_id;
    $urlpath = get_url_path($path);
    $link = URL."/".$urlpath."/".$name;
    $filename = $path ."/". $name;
    
    if(!file_exists($path)){
      mkdir($path);
    }
  
    if (!move_uploaded_file($_FILES['project_img']['tmp_name'], $filename)){
      echo 'Problem: Could not move file to destination directory';
      exit(0);
    }
  
    ini_set('memory_limit', '100M');
    $image->load($filename);
    $image->resize(113,75);
    $image->save($filename);
    ini_set('memory_limit', '16M');
  
    chmod($filename,0755);
    
    //$sql = "UPDATE projects SET project_img ='$name' WHERE project_id=$project_id";
    //$db->execute($sql);
    
    if(file_exists($path.'/'.$_POST['old_img']) && trim($_POST['old_img'])!=''){
      unlink($path.'/'.$_POST['old_img']);
    }
    $img_sql = ",project_img='$name'";
    //echo $img_sql;
  }
  
  $cert_sql = '';
  $handle = new Upload($_FILES['cert']);
  if($handle->file_src_name != '')
  {
    if ($handle->uploaded)
    {
        $handle->Process('../../images/projects/'.$project_id);
        
        if ($handle->processed)
        {
          $sql = 'UPDATE projects SET project_cert ="'.$handle->file_dst_name.'" WHERE project_id='.$project_id;
           if(!mysql_query($sql))
           {
              @unlink('../../images/projects/'.$project_id.'/'.$handle->file_dst_name);
           }
           else
           {
            if(file_exists('../../images/projects/'.$project_id.'/'.$_POST['old_cert']) && trim($_POST['old_cert'])!=''){
              unlink('../../images/projects/'.$project_id.'/'.$_POST['old_cert']);
            }
           }
        }
        $cert_sql = ",project_cert='$name'";
        $handle->clean();
    }
  }
  
  $sql = "UPDATE projects SET
    project_cat_id='$catalogue',
    project_date='$date',
    project_title='$projectname',
    project_area='$area',
    project_owner='$owner',
    project_complete_year='$year',
    project_value='$value'
    $img_sql
    WHERE project_id=$project_id";
  $db->execute($sql);
  
  if($_POST['list1SortOrder']!=''){
    $newImgOrderArray = explode('|',$_POST['list1SortOrder']);
    $i = 1;
    $result = array();
    foreach($newImgOrderArray as $value)
    {
        $sql = 'UPDATE project_img SET img_position='.($i++).' WHERE project_img_id='.$value;
        if(mysql_query($sql))
        {
            $result[] = 'success';
        }
        else
        {
            $result[] = 'fail';
        }
    }
  }
  
  header("Location:index.php");
?>